package com.example.dell.intelligentagriculture.utils;


import android.content.Context;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.dell.intelligentagriculture.R;


/**
 * 轮播图
 */
public class SlidePhoto {
    private int[] imgArray; //存放viewpager的背景图片（三张轮播图）
    private ImageView[] bandimgArray;// 存放viewpager中的imageview
    private ImageView[] tips;//存放小圆点（小圆点控件imageview）
    private LinearLayout viewGroup;


    private ViewPager viewPager;
    Context context;
    public static final int MSG_CHANGE_AUTO_BIND = 100;

    public SlidePhoto(LinearLayout viewGroup, ViewPager viewPager, Context context) {
        this.viewGroup = viewGroup;
        this.viewPager = viewPager;
        this.context = context;
    }

    /**
     * 初始化轮播图的资源
     */
    public ImageView[] initBandRes() {
        imgArray = new int[]{R.mipmap.bana1, R.mipmap.bana2, R.mipmap.bana3};
        //加载bandimgArray  展示三张轮播图
        bandimgArray = new ImageView[3];
        ImageView iv1 = new ImageView(context);
        iv1.setBackgroundResource(imgArray[0]);
        ImageView iv2 = new ImageView(context);
        iv2.setBackgroundResource(imgArray[1]);
        ImageView iv3 = new ImageView(context);
        iv3.setBackgroundResource(imgArray[2]);
        //将三个imageview加载到数组中
        bandimgArray[0] = iv1;
        bandimgArray[1] = iv2;
        bandimgArray[2] = iv3;
        //初始化小圆点数组
        tips = new ImageView[imgArray.length];
        //动态加载小圆点的数组
        for (int i = 0; i < 3; i++) {
            ImageView image = new ImageView(context);
           /* ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(10, 10);
            image.setLayoutParams(layoutParams);*/
            tips[i] = image;
            //加载图片
            if (i == 0) {
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
            //给imageview控件设定参数
            //设定imageview的宽高
            LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            //设定imageview的外边距
            layoutParams1.setMargins(10, 10, 10, 10);
            image.setLayoutParams(layoutParams1);
            viewGroup.addView(image);
        }
        viewPager.setAdapter(new SlideAdapter(bandimgArray));
        viewPager.setCurrentItem(1);

        //图片自动播放
       /* if (tips.length > 0) {
            //延时多长时间后执行发送  sendEmptyMessageDelaye默认运行在子线程中
            //handler.sendEmptyMessage(MSG_CHANGE_AUTO_BIND);
           // new HomeFragment.MyHandler().sendEmptyMessage(MSG_CHANGE_AUTO_BIND)
        }*/
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                changTipsByImg(position % 3);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        return tips;
    }

    /**
     * 动态切换小圆点
     * index： 0 1 2  大图片的索引值
     * <p>
     * index = 2
     */
    private void changTipsByImg(int index) {
        for (int i = 0; i < tips.length; i++) {
            // i ==> 0  1  2  小圆点的索引值
            if (i == index) {
                tips[i].setBackgroundResource(R.mipmap.page_indicator_focused);
            } else {
                tips[i].setBackgroundResource(R.mipmap.page_indicator_unfocused);
            }
        }

    }
}
